/*
DESCRIPTION
This do file creates the tables in the main text.

INPUT
	data/BITTrialAdoption.dta
	data/policyadoption_raw.csv // Hjort et al. (2021) data from http://doi.org/10.3886/E122661V1 

OUTPUT
	tables/sumstats.tex
	tables/reg.tex
	tables/reg_broad.tex
	tables/hjort.tex
*/

*************************************
***TABLE 2: SAMPLE CHARACTERISTICS***
*************************************
{
	clear all 
	use "data/BITTrialAdoption.dta"
	keep if sample==1

	local varlist 	treatmenteffect_maxt_sig treatmenteffect_max_ge1 ///
					WWC_cert staff_same_dpt department_imp staff_senior ///
					comm_preexisted mech_simplification mech_personalmotivation mech_socialcues ///
					policyarea_reve policyarea_regi policyarea_work policyarea_bene policyarea_comm policyarea_hlth policyarea_envi ///
					medium_physicalletter medium_email medium_postcard medium_text medium_website
					
	local insertendrow 	1 "\multicolumn{8}{l}{\textit{Nudge effectiveness}} \\ %" ///
						3 "\\ \multicolumn{8}{l}{\textit{Organizational features}} \\ %" ///
						7 "\\ \multicolumn{8}{l}{\textit{Experimental design}} \\ %" ///
						11 "\\ \multicolumn{8}{l}{\textit{Policy area}} \\ %" ///
						18 "\\ \multicolumn{8}{l}{\textit{Medium}} \\ %" ///
						23 "\hline"
	local rows: word count `varlist'
					
	mata: sumstats=J(`rows'+1,8,"")
	local r=1
	mata: sumstats[`r',1]="\hspace{1em}Max $ t{\geq}1.96$"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Max treatment effect $\geq 1$ pp."
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}City certified by What Works Cities"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}City staff member from trial retained"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Partner city dept. in charge of implementing"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Senior city staff on trial (Director/Chief)"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Communication pre-existed before trial"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Nudge communication uses Simplification"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Nudge communication uses Personal Motivation"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Nudge communication uses Social Cues"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Revenue collection \& debt repayment"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Registration \& regulation compliance"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Workforce \& education"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Take-up of benefits and programs"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Community engagement"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Health"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Environment"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Physical letter"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Email"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Postcard"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Text message"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Website"
	local ++r
	mata: sumstats[`r',1]="Number of trials"
	local ++r

	qui count if trialnumber!=.
	mata: sumstats[`rows'+1,2]="`r(N)'"

	local catlist treatmenteffect_abovemed staff_same_dpt comm_preexisted
	local r=1
	foreach var in `varlist' {
		qui sum `var'
		local freq=string(`r(mean)'*100,"%3.2f")
		mata: sumstats[`r',2]="`freq'"
		
		local col=3
		foreach cat in `catlist' {
			forvalues x=0/1 {
				qui count if `cat'==`x'
				mata: sumstats[`rows'+1,`col']="`r(N)'"
			
				qui sum `var' if `cat'==`x'
				local freq=string(`r(mean)'*100,"%3.2f")
				mata: sumstats[`r',`col']="`freq'"
				
				if `x'==1 {
					qui reg `var' 0.`cat', cl(cityid)
					local pval=2*ttail(e(df_r),abs(_b[0.`cat']/_se[0.`cat']))
					qui tab `var' `cat', matcell(x) exact
					local fisherp=`r(p_exact)'
					mata: x=st_matrix("x")
					mata: minfreq=min(x)
					mata: st_local("minfreq", strofreal(minfreq))
					if (`minfreq' < 5 & `fisherp'<0.05) | (`minfreq' >= 5 & `pval'<0.05) {
						mata: sumstats[`r',`col']="`freq'*"
					}
				}
				local ++col
			}
		}
		local ++r
	}

	mmat2tex sumstats using "tables/sumstats.tex", /// 
			replace ///
			preheader( ///
				\begin{table}[htbp!] \centering \footnotesize \begin{threeparttable} ///
				\caption{Sample characteristics} ///
				\begin{tabular} {l c c c c c c c} ///
				\toprule ///
				& Overall & \multicolumn{2}{c}{Effect size{$>$}median} & \multicolumn{2}{c}{City staff retained} & \multicolumn{2}{c}{Comm. pre-existed} \\ ///
				\cmidrule(lr){2-2} \cmidrule(lr){3-4} \cmidrule(lr){5-6} \cmidrule(lr){7-8} ///
				Frequency in category (\%) & (1) & (2) No & (3) Yes & (4) No & (5) Yes & (6) No & (7) Yes \\ ///
				\hline ///
			) ///
			insertendrow(`insertendrow') ///
			bottom(\bottomrule \end{tabular} ///
				\label{tab:sumstats} ///
				\begin{tablenotes} \scriptsize ///
					\item This table shows the frequencies of trials for each category listed in the leftmost column. Column 1 shows the frequences for all trials. Columns 2 and 3 partition the sample along the median of the maximum effect size in each trial. Columns 4 and 5 consider separately trials for which all the city collaborators from the trial have departed versus trials that have at least one original staff member still working in the same city department. Columns 6 and 7 distinguish between trials that tested nudges in a new communication and those that added nudges to a pre-existing communication that the city had been sending before the trial. ///
					\item *Asterisk indicates that the $ p\text{-value}$ of the difference{$<$}0.05. Standard errors are clustered by city. Except when there are fewer than 5 trials in one of the $ 2{\times}2$ cells, $ p\text{-values}$ are calculated using the two-sided Fisher's exact test instead. ///
				\end{tablenotes} \end{threeparttable} \end{table} ///
			)
}

*********************************************
***TABLE 3: DETERMINANTS OF NUDGE ADOPTION***
*********************************************
{
	clear all
	use "data/BITTrialAdoption.dta"
	keep if sample==1
	
	gen treatmenteffect_maxdiv10=treatmenteffect_max/10
	replace controltakeup=controltakeup/10
	xtset cityid

	gen SampleVar=treatmenteffect_SE^2
	gen mu=1
	gen sigma=1

	program define mylogit 
		args lnf beta0 beta1 beta2 beta3 beta4 beta5 beta6 beta7 sigma mu
		qui replace `lnf' = -ln(1+exp(-( ///
								(SampleVar/(SampleVar+`sigma'^2))*`mu' ///
								+(`sigma'^2/(SampleVar+`sigma'^2))*treatmenteffect_max ///
								+`beta0'+`beta1'+`beta2'+`beta3'+`beta4'+`beta5'+`beta6'+`beta7' ///
							))) ///
							if $ML_y1 == 1
		qui replace `lnf' = (-( ///
								(SampleVar/(SampleVar+`sigma'^2))*`mu' ///
								+(`sigma'^2/(SampleVar+`sigma'^2))*treatmenteffect_max ///
								+`beta0'+`beta1'+`beta2'+`beta3'+`beta4'+`beta5'+`beta6'+`beta7' ///
							)) ///
							- ln(1+exp(-( ///
								(SampleVar/(SampleVar+`sigma'^2))*`mu' ///
								+(`sigma'^2/(SampleVar+`sigma'^2))*treatmenteffect_max ///
								+`beta0'+`beta1'+`beta2'+`beta3'+`beta4'+`beta5'+`beta6'+`beta7' ///
							))) ///
							if $ML_y1 == 0
	end

	local spec1 reg adopted treatmenteffect_maxt_sig treatmenteffect_maxdiv10, cl(cityid)
	local cityfe1 ""
	local polafe1 ""
	local spec2 reg adopted staff_same_dpt pop_abovemed WWC_cert, cl(cityid)
	local cityfe2 ""
	local polafe2 ""
	local spec3 reg adopted comm_preexisted mech_personalmotivation mech_simplification mech_socialcues, cl(cityid)
	local cityfe3 ""
	local polafe3 ""
	local spec4 reg adopted treatmenteffect_maxt_sig treatmenteffect_maxdiv10 staff_same_dpt pop_abovemed WWC_cert comm_preexisted mech_personalmotivation mech_simplification mech_socialcues, cl(cityid)
	local cityfe4 ""
	local polafe4 ""
	local spec5 reghdfe adopted treatmenteffect_maxt_sig treatmenteffect_maxdiv10 staff_same_dpt comm_preexisted mech_personalmotivation mech_simplification mech_socialcues, cl(cityid) absorb(cityid) keepsing
	local cityfe5 "$\checkmark$"
	local polafe5 ""
	local spec6 reghdfe adopted treatmenteffect_maxt_sig treatmenteffect_maxdiv10 staff_same_dpt comm_preexisted mech_personalmotivation mech_simplification mech_socialcues controltakeup medium_online yearselapsed department_imp staff_senior staff_senior_missing, cl(cityid) absorb(cityid policyarea) keepsing
	local cityfe6 "$\checkmark$"
	local polafe6 "$\checkmark$"
	local spec7 logit adopted treatmenteffect_maxt_sig treatmenteffect_maxdiv10 staff_same_dpt pop_abovemed WWC_cert comm_preexisted mech_personalmotivation mech_simplification mech_socialcues, cl(cityid)
	local cityfe7 ""
	local polafe7 ""
	local spec8 ml model lf mylogit (treatmenteffect_maxt_sig: adopted = treatmenteffect_maxt_sig, nocons) (staff_same_dpt: staff_same_dpt, nocons) (pop_abovemed: pop_abovemed, nocons) (WWC_cert: WWC_cert, nocons) (comm_preexisted: comm_preexisted, nocons) (mech_simplification: mech_simplification, nocons) (mech_personalmotivation: mech_personalmotivation, nocons) (mech_socialcues: mech_socialcues, nocons) (sigma: sigma, nocons) (mu: mu, nocons), cl(cityid) maximize seed(1)
	local cityfe8 ""
	local polafe8 ""

	qui logit adopted
	local llcons=`e(ll)'

	local varlist treatmenteffect_maxt_sig treatmenteffect_maxdiv10 staff_same_dpt pop_abovemed WWC_cert comm_preexisted ///
				mech_simplification mech_personalmotivation mech_socialcues controltakeup medium_online yearselapsed department_imp staff_senior ///
				mu sigma
	local varcount: word count `varlist'
	mata: ttab=J((`varcount'+1)*2+6,9,"")
				
	label var treatmenteffect_maxt_sig "Max $ t\geq1.96$" 
	label var treatmenteffect_maxdiv10 "Max treatment effect (10pp.)" 
	label var staff_same_dpt "City staff retained"
	label var pop_abovemed "Above-median city population"
	label var WWC_cert "What Works Cities certified"
	label var comm_preexisted "Communication pre-existed"
	label var medium_online "Uses online mediums"
	label var mech_simplification "\hspace{1em}Simplification \& information"
	label var mech_personalmotivation "\hspace{1em}Personal motivation"
	label var mech_socialcues "\hspace{1em}Social cues"
	label var controltakeup "Control take-up (10\%)"
	label var yearselapsed "Years since trial"
	label var department_imp "City dept. in charge of implementing"
	label var staff_senior "Senior city staff on trial (Director/Chief)"
	label var mu "\hspace{1em}$\mu_0$"
	label var sigma "\hspace{1em}$\sigma_0$"

	local row=1
	foreach var in `varlist' {
		local varlab: variable label `var' 
		mata: ttab[`row',1]="`varlab'"
		local row=`row'+2
	}
	local varlist `varlist' _cons
	mata: ttab[(`varcount'+1)*2-1,1]="Constant"
	mata: ttab[(`varcount'+1)*2+1,1]="Average adoption rate"
	mata: ttab[(`varcount'+1)*2+2,1]="City fixed effects"   
	mata: ttab[(`varcount'+1)*2+3,1]="Policy area fixed effects"
	mata: ttab[(`varcount'+1)*2+4,1]="Number of trials"
	mata: ttab[(`varcount'+1)*2+5,1]="Number of cities"
	mata: ttab[(`varcount'+1)*2+6,1]="(Pseudo-)$ R^2$"

	local col=2
	forvalues s=1/8 {
		mata: mata matsave "tempmat`s'.mmat" ttab, replace
		mata: mata drop ttab
		`spec`s''
		mata: mata matuse "tempmat`s'.mmat" // because reghdfe clears mata		
		
		local row=1
		foreach var in `varlist' {
			cap local b=string(_b[`var'],"%4.2f")
			if `s'==8 {
				cap local b=string([`var']_b[`var'],"%4.2f")
			}
		  
			if _rc==0 {
				if "`var'"=="sigma" {
					local b=string(abs(`b'),"%4.2f")	
				}
				cap local `var'=`b'
				mata: ttab[`row'*2-1,`col']="`b'"
				cap local se=string(_se[`var'],"%4.2f")
				if `s'==8 {
					local se=string([`var']_se[`var'],"%4.2f")
				}
				mata: ttab[`row'*2,`col']="(`se')"
			}

			local ++row
		}
		
		mata: ttab[(`varcount'+1)*2+2,`col']="`cityfe`s''"
		mata: ttab[(`varcount'+1)*2+3,`col']="`polafe`s''"

		local x=`e(N)'
		mata: ttab[(`varcount'+1)*2+4,`col']="`x'"
		local x=`e(N_clust)'
		mata: ttab[(`varcount'+1)*2+5,`col']="`x'"

		cap local r2=string(`e(r2)',"%3.2f")
		if _rc==0 {
			mata: ttab[(`varcount'+1)*2+6,`col']="`r2'"
		}
		cap local r2=string(`e(r2_p)',"%3.2f")
		if _rc==0 {
			mata: ttab[(`varcount'+1)*2+6,`col']="`r2'"
		}
		if `s'==8 {
			local r2=string(1-(`e(ll)'/`llcons'),"%3.2f")
			mata: ttab[(`varcount'+1)*2+6,`col']="`r2'"
		}

		qui sum adopted if e(sample)==1
		local avg=string(`r(mean)',"%3.2f")
		mata: ttab[(`varcount'+1)*2+1,`col']="`avg'"
		
		local ++col
	}

	qui sum SampleVar, d
	local sampvarmean=string(`r(mean)',"%4.2f")
	local sampvarmed=string(`r(p50)',"%4.2f")
	local mean=string(`sigma'^2/(`r(mean)'+`sigma'^2),"%3.2f")
	local med=string(`sigma'^2/(`r(p50)'+`sigma'^2),"%3.2f")

	mmat2tex ttab using "tables/reg.tex", /// 
				replace ///
				preheader(\begin{table}[htbp!] \footnotesize \centering \begin{threeparttable} ///
				\caption{Determinants of nudge adoptions} \begin{tabular} {l c c c c c c c c} \toprule ///
					& \multicolumn{6}{c}{OLS} & Logit & ML \\ ///
					\cmidrule(lr){2-7} \cmidrule(lr){8-8} \cmidrule(lr){9-9} ///
					Dep. var.: Nudge adopted (0/1) & (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8) \\ ///
					\hline ///
				) ///
				insertendrow(13 "\multicolumn{9}{l}{\textit{Mechanism}} \\" 29 "\multicolumn{9}{l}{\textit{Prior parameters}} \\" 35 "\hline") ///
				bottom(\bottomrule \end{tabular} ///
				\label{tab:reg} ///
				\begin{tablenotes} \scriptsize ///
				\item Standard errors clustered by city are shown in parentheses. ///
					Policy area fixed effects includes a dummy each of the policy areas ///
					(Community engagement; Environment; Health; Registration \& regulation compliance; ///
					Revenue collection \& debt repayment; Take-up of benefits and programs; and Workforce \& education). ///
					3 trials are missing the data on the seniority of the city staff member working on the trial (Column 6); ///
					these trials are included with an indicator for missing. ///
					Column 8 estimates the model from Section 3 via maximum likelihood. ///
					The model specifies the distribution of the policy-maker's prior ///
						on the percentage point effectiveness of the nudge as $ N(\mu_{0},\sigma_{0}^2).$ ///
					The policy-maker updates after observing the treatment effect of the nudge from the trial. ///
					The weight placed on the signal is $ \sigma_{0}^2/(\sigma_{s}^2 + \sigma_{0}^2),$ ///
						where $ \sigma_{s}^2$ is the sampling variance or the square of the standard error, ///
						and the weight on the prior is $ \sigma_{s}^2/(\sigma_{s}^2 + \sigma_{0}^2).$ ///
					The average sampling variance is `sampvarmean', which gives a weight on the signal of `mean', ///
						and the median is `sampvarmed', which provides a signal weight of `med'. ///
				\end{tablenotes} \end{threeparttable} \end{table})
	
	forvalues s=1/8 {
			erase "tempmat`s'.mmat"
	}
}

*********************************************
***TABLE 4: COMPARISON WITH BROAD ADOPTION***
*********************************************
{ 
	clear all
	use "data/BITTrialAdoption.dta"
	keep if sample==1

	replace adopted_broaddefn=0 if missing(adopted_broaddefn)
	replace treatmenteffect_max=treatmenteffect_max/10

	local row=1
	mata: broad=J(24,4,"")
	mata: broad[`row',1]="Max $ t\geq1.96$" 
	local row=`row'+2
	mata: broad[`row',1]="Max treatment effect (10pp.)" 
	local row=`row'+2
	mata: broad[`row',1]="City staff retained" 
	local row=`row'+2
	mata: broad[`row',1]="Above-median city population" 
	local row=`row'+2
	mata: broad[`row',1]="What Works Cities certified" 
	local row=`row'+2
	mata: broad[`row',1]="Communication pre-existed" 
	local row=`row'+2
	mata: broad[`row',1]="\hspace{1em}Simplification \& information" 
	local row=`row'+2
	mata: broad[`row',1]="\hspace{1em}Personal motivation"
	local row=`row'+2
	mata: broad[`row',1]="\hspace{1em}Social cues" 
	local row=`row'+2
	mata: broad[`row',1]="Constant" 
	local row=`row'+2
	mata: broad[`row',1]="Average adoption rate" 
	local row=`row'+1
	mata: broad[`row',1]="Number of trials" 
	local row=`row'+1
	mata: broad[`row',1]="Number of cities"
	local row=`row'+1
	mata: broad[`row',1]="$ R^2$"

	reg adopted treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert comm_preexisted mech_simplification mech_personalmotivation mech_socialcues, cl(cityid)
	local row=1
	foreach var in treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert comm_preexisted mech_simplification mech_personalmotivation mech_socialcues _cons {
		local b=string(_b[`var'],"%4.2f")
		local se=string(_se[`var'],"%4.2f")
		mata: broad[`row'*2-1,2]="`b'"
		mata: broad[`row'*2,2]="(`se')"
		local ++row
	}
	mata: broad[22,2]="`e(N)'"
	mata: broad[23,2]="`e(N_clust)'"
	local r2=string(`e(r2)',"%3.2f")
	mata: broad[24,2]="`r2'"
	qui sum adopted if e(sample)==1
	local avg=string(`r(mean)',"%4.2f")
	mata: broad[21,2]="`avg'"

	reg adopted_broaddefn treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert comm_preexisted mech_simplification mech_personalmotivation mech_socialcues, cl(cityid)
	local row=1
	foreach var in treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert comm_preexisted mech_simplification mech_personalmotivation mech_socialcues _cons {
		local b=string(_b[`var'],"%4.2f")
		local se=string(_se[`var'],"%4.2f")
		mata: broad[`row'*2-1,3]="`b'"
		mata: broad[`row'*2,3]="(`se')"
		local ++row
	}
	mata: broad[22,3]="`e(N)'"
	mata: broad[23,3]="`e(N_clust)'"
	local r2=string(`e(r2)',"%3.2f")
	mata: broad[24,3]="`r2'"
	qui sum adopted_broaddefn if e(sample)==1
	local avg=string(`r(mean)',"%4.2f")
	mata: broad[21,3]="`avg'"

	qui reg adopted treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert comm_preexisted mech_simplification mech_personalmotivation mech_socialcues
	est sto a
	qui reg adopted_broaddefn treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert comm_preexisted mech_simplification mech_personalmotivation mech_socialcues
	est sto b

	suest a b, cl(cityid)

	local row=1
	foreach var in treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert comm_preexisted mech_simplification mech_personalmotivation mech_socialcues _cons {
		lincom [a_mean]`var'-[b_mean]`var'
		local b=string(`r(estimate)',"%4.2f")
		local se=string(`r(se)',"%4.2f")
		mata: broad[`row'*2-1,4]="`b'"
		mata: broad[`row'*2,4]="(`se')"
		local ++row
	}

	mmat2tex broad using "tables/reg_broad.tex", /// 
				replace ///
				preheader(\begin{table}[htbp!] \centering \footnotesize \begin{threeparttable} ///
				\caption{Comparison of specific nudge adoption and broad adoption} \begin{tabular} {l c c c} \toprule ///
					& Nudge adoption & Broad adoption & Difference \\ ///
					Dep. var.: Adoption (0/1, OLS)  & (1) & (2) & (3) \\ ///
					\hline ///
				) ///
				insertendrow(13 "\multicolumn{4}{l}{\textit{Mechanism}} \\" 21 "\hline") ///
				bottom(\bottomrule \end{tabular} ///
				\label{tab:reg_broad} ///
				\begin{tablenotes} \scriptsize ///
				\item ///
					Standard errors clustered by city are shown in parentheses. ///
					In Column 1, the dependent variable is the same binary indicator from Table 2 ///
						for whether the city adopted the specific nudge in the trial. ///
						Column 1 replicates the baseline specification of Column 4 in Table 2. ///
					In Column 2, the dependent variable is a binary indicator ///
						for whether the city broadly adopted a similar nudge ///
						or the method of experimentation in other contexts. ///					
				\end{tablenotes} \end{threeparttable} \end{table})
}

*******************************************
***TABLE 5: HJORT ET AL. (2021) ANALYSIS***
*******************************************
{ 
	clear all
	import delimited "data/policyadoption_raw.csv"

	* Generate additional adoption categories
	egen implemented1_sum=rowtotal(implemented1_ontime implemented1_audits implemented1_social implemented1_before implemented1_letter) if implemented1!=.
	gen byte implemented1_allmech=(implemented1_ontime + implemented1_audits + implemented1_social == 3) if implemented1_sum!=.
	label var implemented1_allmech "Adopted all 3 mechanisms (ontime, audits, social)"
	gen byte implemented1_2of3 = (implemented1_ontime + implemented1_audits + implemented1_social >=2) if implemented1_sum!=.
	label var implemented1_2of3 "Adopted at least 2 of ontime, audits/fine, and social norms"

	bysort munic_id: gen n=_n
	* Take MAX of adoption measures (since both mayor and city staff included; sometimes non-agreement, so take max)
	foreach var in implemented1_allmech implemented1_2of3 {
		egen `var'_max=max(`var'), by(munic_id)
		replace `var'_max=. if n!=1 // keep only one obs per city
	}

	mata
		hjortetal=J(21,5,"")
		hjortetal[1,1]="$ f$"
		hjortetal[3,1]="$ f_{pe}$ (pre-existing)"
		hjortetal[5,1]="$ f_{diag}$ (diagonal)"
		hjortetal[7,1]="MSE"
		hjortetal[8,1]="$ f$"
		hjortetal[10,1]="$ f_{pe}$ (pre-existing)"
		hjortetal[12,1]="$ f_{diag}$ (diagonal)"
		hjortetal[14,1]="MSE"
		hjortetal[15,1]="$ f$"
		hjortetal[17,1]="$ f_{pe}$ (pre-existing)"
		hjortetal[19,1]="$ f_{diag}$ (diagonal)"
		hjortetal[21,1]="MSE"
	end

	local row=1
	foreach nudge in implemented1_allmech implemented1_2of3 implemented1_social {

		*Control group moments
		qui tab `nudge' implemented1_letter if treatment_assignment==0, matcell(Pc)
		mat Pc=Pc/`r(N)'
		local Pc_nn=Pc[1,1]
		local Pc_ln=Pc[1,2]
		local Pc_nd=Pc[2,1]
		local Pc_ld=Pc[2,2]

		*Treatment group moments
		qui tab `nudge' implemented1_letter if treatment_assignment==1, matcell(Pt) 
		mat Pt=Pt/`r(N)'
		local Pt_nn=Pt[1,1]
		local Pt_ln=Pt[1,2]
		local Pt_nd=Pt[2,1]
		local Pt_ld=Pt[2,2]	

		*ToT rate
		qui reg attended treatment_assignment
		local ToT=_b[treatment_assignment]
		
		local col=2
		
		/* Baseline
		Equations 1,2,3, L^2 norm minimum distance (equiv to OLS)
		x*-Pc_ln + Pc_nn          = Pt_ln - Pc_ln
		x*(-Pc_nd + Pc_nn) 		  = Pt_nd - Pc_nd
		x*(Pc_nd + Pc_nn + Pc_ln) = Pt_ld - Pc_ld
		*/
		mata: X1 = `Pc_nn'-`Pc_ln' \ -`Pc_nd'+`Pc_nn' \ `Pc_ln'+`Pc_nd'+`Pc_nn'
		mata: Y1 = `Pt_ln'-`Pc_ln' \ `Pt_nd'-`Pc_nd' \ `Pt_ld'-`Pc_ld'
		mata: XtX1=X1'X1
		mata: XtY1=X1'Y1
		mata: XtXinv1=1/XtX1
		mata: f1=XtXinv1*XtY1
		mata: st_local("fx1",strofreal(f1[1,1]))
		local str=string(`fx1'/`ToT',"%4.3f")
		mata: hjortetal[`row',`col']="`str'"

		* Predicted proportions
		local Prt_nn1 = `Pc_nn'*(1-3*`fx1')
		local Prt_nd1 = `Pc_nd'*(1-`fx1') + `Pc_nn'*`fx1'
		local Prt_ln1 = `Pc_ln'*(1-`fx1') + `Pc_nn'*`fx1'
		local Prt_ld1 = `Pc_ld' + `Pc_ln'*`fx1' + `Pc_nn'*`fx1' + `Pc_nd'*`fx1'
		qui assert abs(`Prt_nn1'+`Prt_nd1'+`Prt_ln1'+`Prt_ld1'-1)<0.00001
		local MSE=((100*`Pt_nn'-100*`Prt_nn1')^2 + (100*`Pt_nd'-100*`Prt_nd1')^2 + (100*`Pt_ln'-100*`Prt_ln1')^2 + (100*`Pt_ld'-100*`Prt_ld1')^2)/4
		local str=string(`MSE',"%5.3f")
		mata: hjortetal[`row'+6,`col']="`str'"
		local ++col
		
		/* + Pre-existing only
		Assumption ABC, Equations 1,2,3, L^2 norm minimum distance (equiv to OLS)
		f(ln->ld)*-Pc_ln + x*Pc_nn            = Pt_ln - Pc_ln
		f(ln->ld)* 0     + x*(-Pc_nd + Pc_nn) = Pt_nd - Pc_nd
		f(ln->ld)* Pc_ln + x*(Pc_nd + Pc_nn)  = Pt_ld - Pc_ld
		*/
		mata: X = -`Pc_ln', `Pc_nn' \ 0, -`Pc_nd'+`Pc_nn' \ `Pc_ln', `Pc_nd'+`Pc_nn'
		mata: Y = `Pt_ln'-`Pc_ln' \ `Pt_nd'-`Pc_nd' \ `Pt_ld'-`Pc_ld'
		mata: XtX=X'X
		mata: XtY=X'Y
		mata: XtXinv = XtX[2,2] , -XtX[1,2] \ -XtX[2,1], XtX[1,1]
		mata: XtXinv = (1/(XtX[1,1]*XtX[2,2] - XtX[1,2]*XtX[2,1]))*XtXinv
		mata: f=XtXinv*XtY
		mata: st_local("fpe",strofreal(f[1,1]))
		mata: st_local("fx",strofreal(f[2,1]))
		local str=string(`fx'/`ToT',"%4.3f")
		mata: hjortetal[`row',`col']="`str'"
		local str=string(`fpe'/`ToT',"%4.3f")
		mata: hjortetal[`row'+2,`col']="`str'"
		
		*Predicted proportions
		local Prt_nn2 = `Pc_nn'*(1-3*`fx')
		local Prt_nd2 = `Pc_nd'*(1-`fx') + `Pc_nn'*`fx'
		local Prt_ln2 = `Pc_ln'*(1-`fpe') + `Pc_nn'*`fx'
		local Prt_ld2 = `Pc_ld' + `Pc_ln'*`fpe' + `Pc_nn'*`fx' + `Pc_nd'*`fx'
		qui assert abs(`Prt_nn2'+`Prt_nd2'+`Prt_ln2'+`Prt_ld2'-1)<0.00001
		local MSE=((100*`Pt_nn'-100*`Prt_nn2')^2 + (100*`Pt_nd'-100*`Prt_nd2')^2 + (100*`Pt_ln'-100*`Prt_ln2')^2 + (100*`Pt_ld'-100*`Prt_ld2')^2)/4
		local str=string(`MSE',"%5.3f")
		mata: hjortetal[`row'+6,`col']="`str'"
		local ++col

		/* + Diag only
		f_nn->ld free
		f*-Pc_ln + f* Pc_nn + f*0	   + f* 0     + f(nn->ld)* 0     = Pt_ln - Pc_ln
		f* 0     + f*0		+ f*-Pc_nd + f* Pc_nn + f(nn->ld)* 0     = Pt_nd - Pc_nd
		f* Pc_ln + f*0		+ f* Pc_nd + f* 0     + f(nn->ld)* Pc_nn = Pt_ld - Pc_ld
		*/
		mata: X = 0,`Pc_nn'-`Pc_ln' \ 0, `Pc_nn'-`Pc_nd' \ `Pc_nn', `Pc_ln'+`Pc_nd'
		mata: Y = `Pt_ln'-`Pc_ln' \ `Pt_nd'-`Pc_nd' \ `Pt_ld'-`Pc_ld'
		mata: XtX=X'X
		mata: XtY=X'Y
		mata: XtXinv = XtX[2,2] , -XtX[1,2] \ -XtX[2,1], XtX[1,1]
		mata: XtXinv = (1/(XtX[1,1]*XtX[2,2] - XtX[1,2]*XtX[2,1]))*XtXinv
		mata: f=XtXinv*XtY
		mata: st_local("fx",strofreal(f[1,1]))
		mata: st_local("f",strofreal(f[2,1]))
		local str=string(`f'/`ToT',"%4.3f")
		mata: hjortetal[`row',`col']="`str'"
		local str=string(`fx'/`ToT',"%4.3f")
		mata: hjortetal[`row'+4,`col']="`str'"
		
		*Predicted proportions
		local Prt_nn3 = `Pc_nn' - `Pc_nn'*`f'	- `Pc_nn'*`f'	- `Pc_nn'*`fx'
		local Prt_nd3 = `Pc_nd' 					+ `Pc_nn'*`f' 					- `Pc_nd'*`f'
		local Prt_ln3 = `Pc_ln' + `Pc_nn'*`f' 															- `Pc_ln'*`f'
		local Prt_ld3 = `Pc_ld' 										+ `Pc_nn'*`fx'	+ `Pc_nd'*`f'	+ `Pc_ln'*`f'
		qui assert (`Prt_nn3'+`Prt_nd3'+`Prt_ln3'+`Prt_ld3'-1<0.000001)
		local MSE=((100*`Pt_nn'-100*`Prt_nn3')^2 + (100*`Pt_nd'-100*`Prt_nd3')^2 + (100*`Pt_ln'-100*`Prt_ln3')^2 + (100*`Pt_ld'-100*`Prt_ld3')^2)/4
		local str=string(`MSE',"%5.3f")
		mata: hjortetal[`row'+6,`col']="`str'"
		local ++col
		
		/* Both Pre-existing and Diag
		Assumption AC(E)
		f(ln->ld)*-Pc_ln + f(nn->ln)* Pc_nn         + f(nn->ld)* 0     = Pt_ln - Pc_ln
		f(ln->ld)* 0     + f(nn->ln)*(-Pc_nd+Pc_nn) + f(nn->ld)* 0     = Pt_nd - Pc_nd
		f(ln->ld)* Pc_ln + f(nn->ln)* Pc_nd         + f(nn->ld)* Pc_nn = Pt_ld - Pc_ld
		*/
		mata: X = -`Pc_ln', `Pc_nn', 0 \ 0, -`Pc_nd'+`Pc_nn', 0 \ `Pc_ln', `Pc_nd', `Pc_nn'
		mata: Y = `Pt_ln'-`Pc_ln' \ `Pt_nd'-`Pc_nd' \ `Pt_ld'-`Pc_ld'
		mata: f=lusolve(X,Y)
		mata: st_local("fb",strofreal(f[2,1]))
		local str=string(`fb'/`ToT',"%4.3f")
		mata: hjortetal[`row',`col']="`str'"
		mata: st_local("fpe",strofreal(f[1,1]))
		local str=string(`fpe'/`ToT',"%4.3f")
		mata: hjortetal[`row'+2,`col']="`str'"
		mata: st_local("fd",strofreal(f[3,1]))
		local str=string(`fd'/`ToT',"%4.3f")
		mata: hjortetal[`row'+4,`col']="`str'"
		
		*Predicted proportions
		local Prt_nd4 = `Pc_nd'+`fb'*(-`Pc_nd'+`Pc_nn')
		local Prt_ln4 = `Pc_ln'+`fpe'*(-`Pc_ln')+`fb'*`Pc_nn'
		local Prt_ld4 = `Pc_ld'+`fpe'*`Pc_ln'+`fb'*`Pc_nd'+`fd'*`Pc_nn'
		local Prt_nn4 = `Pc_nn'-`Pc_nn'*(`fd'+`fb'+`fb')
		qui assert (`Prt_nn4'+`Prt_nd4'+`Prt_ln4'+`Prt_ld4'-1<0.000001)
		local MSE=((100*`Pt_nn'-100*`Prt_nn4')^2 + (100*`Pt_nd'-100*`Prt_nd4')^2 + (100*`Pt_ln'-100*`Prt_ln4')^2 + (100*`Pt_ld'-100*`Prt_ld4')^2)/4
		local str=string(`MSE',"%5.3f")
		mata: hjortetal[`row'+6,`col']="`str'"
		
		local row=`row'+7
	}

	{ // Bootstrap SEs
		local bsnum 1000
		
		set seed 1
		
		mat bsA=J(`bsnum',12,.)
		mat colnames bsA = fb1 fpe1 fd1 fb2 fpe2 fd2 fb3 fpe3 fd3 fb4 fpe4 fd4
		mat bsB=J(`bsnum',12,.)
		mat colnames bsB = fb1 fpe1 fd1 fb2 fpe2 fd2 fb3 fpe3 fd3 fb4 fpe4 fd4
		mat bsC=J(`bsnum',12,.)
		mat colnames bsC = fb1 fpe1 fd1 fb2 fpe2 fd2 fb3 fpe3 fd3 fb4 fpe4 fd4
		
		forvalues bs=1/`bsnum' {
			if mod(`bs',100)==0 {
				di "Bootstrap round `bs' of `bsnum'"
			}
			preserve
				bsample, cl(munic_id)
				
				local list A B C
				local x=1
				foreach nudge in implemented1_allmech implemented1_2of3 implemented1_social {
					local defn: word `x' of `list'
					
					*Control group moments
					qui tab `nudge' implemented1_letter if treatment_assignment==0, matcell(Pc)
					mat Pc=Pc/`r(N)'
					local Pc_nn=Pc[1,1]
					local Pc_ln=Pc[1,2]
					local Pc_nd=Pc[2,1]
					local Pc_ld=Pc[2,2]

					*Treatment group moments
					qui tab `nudge' implemented1_letter if treatment_assignment==1, matcell(Pt) 
					mat Pt=Pt/`r(N)'
					local Pt_nn=Pt[1,1]
					local Pt_ln=Pt[1,2]
					local Pt_nd=Pt[2,1]
					local Pt_ld=Pt[2,2]	

					*ToT rate
					qui reg attended treatment_assignment
					local ToT=_b[treatment_assignment]
					
					local col=2
					
					/* Baseline
					Equations 1,2,3, L^2 norm minimum distance (equiv to OLS)
					x*-Pc_ln + Pc_nn          = Pt_ln - Pc_ln
					x*(-Pc_nd + Pc_nn) 		  = Pt_nd - Pc_nd
					x*(Pc_nd + Pc_nn + Pc_ln) = Pt_ld - Pc_ld
					*/
					mata: X1 = `Pc_nn'-`Pc_ln' \ -`Pc_nd'+`Pc_nn' \ `Pc_ln'+`Pc_nd'+`Pc_nn'
					mata: Y1 = `Pt_ln'-`Pc_ln' \ `Pt_nd'-`Pc_nd' \ `Pt_ld'-`Pc_ld'
					mata: XtX1=X1'X1
					mata: XtY1=X1'Y1
					mata: XtXinv1=1/XtX1
					mata: f1=XtXinv1*XtY1
					mata: st_local("fx1",strofreal(f1[1,1]))
					mat bs`defn'[`bs',1]=`fx1'/`ToT'
					
					/* + Pre-existing only
					Assumption ABC, Equations 1,2,3, L^2 norm minimum distance (equiv to OLS)
					f(ln->ld)*-Pc_ln + x*Pc_nn            = Pt_ln - Pc_ln
					f(ln->ld)* 0     + x*(-Pc_nd + Pc_nn) = Pt_nd - Pc_nd
					f(ln->ld)* Pc_ln + x*(Pc_nd + Pc_nn)  = Pt_ld - Pc_ld
					*/
					mata: X = -`Pc_ln', `Pc_nn' \ 0, -`Pc_nd'+`Pc_nn' \ `Pc_ln', `Pc_nd'+`Pc_nn'
					mata: Y = `Pt_ln'-`Pc_ln' \ `Pt_nd'-`Pc_nd' \ `Pt_ld'-`Pc_ld'
					mata: XtX=X'X
					mata: XtY=X'Y
					mata: XtXinv = XtX[2,2] , -XtX[1,2] \ -XtX[2,1], XtX[1,1]
					mata: XtXinv = (1/(XtX[1,1]*XtX[2,2] - XtX[1,2]*XtX[2,1]))*XtXinv
					mata: f=XtXinv*XtY
					mata: st_local("fpe",strofreal(f[1,1]))
					mata: st_local("fx",strofreal(f[2,1]))
					mat bs`defn'[`bs',4]=`fx'/`ToT'
					mat bs`defn'[`bs',5]=`fpe'/`ToT'
									
					/* + Diag only
					f_nn->ld free
					f*-Pc_ln + f* Pc_nn + f*0	   + f* 0     + f(nn->ld)* 0     = Pt_ln - Pc_ln
					f* 0     + f*0		+ f*-Pc_nd + f* Pc_nn + f(nn->ld)* 0     = Pt_nd - Pc_nd
					f* Pc_ln + f*0		+ f* Pc_nd + f* 0     + f(nn->ld)* Pc_nn = Pt_ld - Pc_ld
					*/
					mata: X = 0,`Pc_nn'-`Pc_ln' \ 0, `Pc_nn'-`Pc_nd' \ `Pc_nn', `Pc_ln'+`Pc_nd'
					mata: Y = `Pt_ln'-`Pc_ln' \ `Pt_nd'-`Pc_nd' \ `Pt_ld'-`Pc_ld'
					mata: XtX=X'X
					mata: XtY=X'Y
					mata: XtXinv = XtX[2,2] , -XtX[1,2] \ -XtX[2,1], XtX[1,1]
					mata: XtXinv = (1/(XtX[1,1]*XtX[2,2] - XtX[1,2]*XtX[2,1]))*XtXinv
					mata: f=XtXinv*XtY
					mata: st_local("fx",strofreal(f[1,1]))
					mata: st_local("f",strofreal(f[2,1]))
					mat bs`defn'[`bs',7]=`f'/`ToT'
					mat bs`defn'[`bs',9]=`fx'/`ToT'
					
					/* Both Pre-existing and Diag
					Assumption AC(E)
					f(ln->ld)*-Pc_ln + f(nn->ln)* Pc_nn         + f(nn->ld)* 0     = Pt_ln - Pc_ln
					f(ln->ld)* 0     + f(nn->ln)*(-Pc_nd+Pc_nn) + f(nn->ld)* 0     = Pt_nd - Pc_nd
					f(ln->ld)* Pc_ln + f(nn->ln)* Pc_nd         + f(nn->ld)* Pc_nn = Pt_ld - Pc_ld
					*/
					mata: X = -`Pc_ln', `Pc_nn', 0 \ 0, -`Pc_nd'+`Pc_nn', 0 \ `Pc_ln', `Pc_nd', `Pc_nn'
					mata: Y = `Pt_ln'-`Pc_ln' \ `Pt_nd'-`Pc_nd' \ `Pt_ld'-`Pc_ld'
					mata: f=lusolve(X,Y)
					mata: st_local("fb",strofreal(f[2,1]))
					mata: st_local("fpe",strofreal(f[1,1]))
					mata: st_local("fd",strofreal(f[3,1]))
					mat bs`defn'[`bs',10]=`fb'/`ToT'
					mat bs`defn'[`bs',11]=`fpe'/`ToT'
					mat bs`defn'[`bs',12]=`fd'/`ToT'
					
					local ++x
			}
			restore
		}
		
		local col=2
		local row=2
		foreach defn in A B C {
			preserve
				clear
				svmat bs`defn', names(col)
				forvalues model=1/4 {
					qui sum fb`model'
					if `r(N)'>0 {
						local str=string(`r(sd)',"%5.3f")
						mata: hjortetal[`row',1+`model']="(`str')"
					}
					qui sum fpe`model'
					if `r(N)'>0 {
						local str=string(`r(sd)',"%5.3f")
						mata: hjortetal[`row'+2,1+`model']="(`str')"
					}
					qui sum fd`model'
					if `r(N)'>0 {
						local str=string(`r(sd)',"%5.3f")
						mata: hjortetal[`row'+4,1+`model']="(`str')"
					}
				}
			restore
			local row=`row'+7
		}
	}

	mmat2tex hjortetal using "tables/hjort.tex", /// 
		replace ///
		preheader(\begin{table}[htbp!] \centering \footnotesize \begin{threeparttable} ///
			\caption{Hjort et al. (2021) policy adoption experiment: Persuasion rates} ///
			\begin{tabular} {l c c c c} \toprule /// 
			\textit{Persuasion rates (treatment-on-treated)} & (1) & (2) & (3) & (4) \\ ///
			\hline ///
			\multicolumn{5}{l}{\textit{\textbf{Nudge adoption definition: All 3 mechanisms}}} \\ ///
			) ///
		///
		insertendrow(8 "\\ \multicolumn{5}{l}{\textit{\textbf{Nudge adoption definition: $ \geq2$ of 3 mechanisms}}} \\" ///
						15 "\\ \multicolumn{5}{l}{\textit{\textbf{Nudge adoption definition: Social cues}}} \\") ///
		bottom( ///
			\bottomrule \end{tabular} ///
			\label{tab:hjort} ///
			\begin{tablenotes} \scriptsize ///
			\item 	This table shows the treatment-on-treated persuasion rates estimated from the model ///
						in Figure 12. ///
					The 3 mechanisms mentioned in the template for the tax reminder letter are ///
					the due date, the threat of audits or fines, and social norm language. ///
					MSE is the mean squared error in the 4 moments for the treatment group. ///
					The MSE for (4) is 0 since the model is exactly identified. ///
				Standard errors from 1000 bootstrap samples (resampled at the municipal level) are shown in parentheses. ///
			\end{tablenotes} \end{threeparttable} \end{table})
}